{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tce3stUlHN0L"
      },
      "source": [
        "##### Copyright 2025 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "tuOe1ymfHZPu"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qFdPvlXBOdUN"
      },
      "source": [
        "# Gemini API: Video prompting with REST"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MfBg1C5NB3X0"
      },
      "source": [
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/rest/Video_REST.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xHxb-dlhMIzW"
      },
      "source": [
        "This notebook provides quick code examples that show you how  to prompt the Gemini API using a video file with `curl`. In this case, you'll use a short clip of [Big Buck Bunny](https://peach.blender.org/about/).\n",
        "\n",
        "You can run this in Google Colab, or you can copy/paste the `curl` commands into your terminal."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "niHjc3x0xfe6"
      },
      "source": [
        "## Set up the environment\n",
        "\n",
        "To run this notebook, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you are running in a different environment, you can store your key in an environment variable. See [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) to learn more."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0baD3tIsvJfU"
      },
      "source": [
        "### Authentication Overview\n",
        "\n",
        "**Important:** The File API uses API keys for authentication and access. Uploaded files are associated with the API key's cloud project. Unlike other Gemini APIs that use API keys, your API key also grants access data you've uploaded to the File API, so take extra care in keeping your API key secure. For best practices on securing API keys, refer to the [API console support center](https://support.google.com/googleapi/answer/6310037)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "wltbMJLIIXGk"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from google.colab import userdata\n",
        "\n",
        "os.environ['GOOGLE_API_KEY'] = userdata.get('GOOGLE_API_KEY')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-4QsOUdkxml1"
      },
      "source": [
        "Install `jq` to help with processing of JSON API responses."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "AfDx324jxk7a"
      },
      "outputs": [],
      "source": [
        "!apt install -q jq"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "w5i8tMZKIeUu"
      },
      "source": [
        "## Use a video file with the Gemini API\n",
        "\n",
        "The Gemini API accepts video file formats through the File API. The File API accepts files under 2GB in size and can store up to 20GB of files per project. Files last for 2 days and cannot be downloaded from the API. For this example, you will use the short film \"Big Buck Bunny\".\n",
        "\n",
        "> \"Big Buck Bunny\" is (C) Copyright 2008, Blender Foundation / www.bigbuckbunny.org and [licensed](https://peach.blender.org/about/) under the [Creative Commons Attribution 3.0](http://creativecommons.org/licenses/by/3.0/) License.\n",
        "\n",
        "Note: In Colab, you can also [upload your own files](https://github.com/google-gemini/cookbook/blob/main/examples/Upload_files_to_Colab.ipynb) to use."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2Qjwh6brwre6"
      },
      "outputs": [],
      "source": [
        "!wget https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "B3gCFZwrvQMC"
      },
      "source": [
        "With the video file now available locally, generate some metadata that you will use in subsequent steps."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "i-S7T8d9-ymb"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "./BigBuckBunny_320x180.mp4 video/mp4 64657027\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        "\n",
        "VIDEO_PATH=\"./BigBuckBunny_320x180.mp4\"\n",
        "DISPLAY_NAME=\"Big Buck Bunny\"\n",
        "\n",
        "# Auto-detect the metadata needed when you upload the video.\n",
        "MIME_TYPE=$(file -b --mime-type \"${VIDEO_PATH}\")\n",
        "NUM_BYTES=$(wc -c < \"${VIDEO_PATH}\")\n",
        "\n",
        "echo $VIDEO_PATH $MIME_TYPE $NUM_BYTES\n",
        "\n",
        "# Colab doesn't allow sharing shell variables between cells, so save them.\n",
        "cat >./vars.sh <<-EOF\n",
        "  export BASE_URL=\"https://generativelanguage.googleapis.com\"\n",
        "  export DISPLAY_NAME=\"${DISPLAY_NAME}\"\n",
        "  export VIDEO_PATH=${VIDEO_PATH}\n",
        "  export MIME_TYPE=${MIME_TYPE}\n",
        "  export NUM_BYTES=${NUM_BYTES}\n",
        "EOF"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "W6T6B8qIOAYO"
      },
      "source": [
        "### Start the upload task\n",
        "\n",
        "Media uploads in the File API are resumable, so the first step is to define an upload task. This initial request gives you a reference you can use for subsequent upload operations, and allows you to query the status of the upload before sending data, in case of network issues during the data transfer.\n",
        "\n",
        "The API returns the upload URL in the `x-goog-upload-url` header, so take note of that in the response headers - you will send the payload data to this URL.\n",
        "\n",
        "No payload data (video bytes) are sent in this initial request."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "BuOqaSVLN-w3"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "HTTP/2 200 \r\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        ". vars.sh\n",
        "\n",
        "# Create the \"new upload\" request by providing the relevant metadata.\n",
        "curl \"${BASE_URL}/upload/v1beta/files?key=${GOOGLE_API_KEY}\" \\\n",
        "  -D upload-header.tmp \\\n",
        "  -H \"X-Goog-Upload-Protocol: resumable\" \\\n",
        "  -H \"X-Goog-Upload-Command: start\" \\\n",
        "  -H \"X-Goog-Upload-Header-Content-Length: ${NUM_BYTES}\" \\\n",
        "  -H \"X-Goog-Upload-Header-Content-Type: ${MIME_TYPE}\" \\\n",
        "  -H \"Content-Type: application/json\" \\\n",
        "  -d \"{'file': {'display_name': '${DISPLAY_NAME}'}}\" 2>/dev/null\n",
        "\n",
        "# Print the status.\n",
        "head -1 upload-header.tmp"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PUcOT-vyTt5s"
      },
      "source": [
        "### Upload video data\n",
        "\n",
        "Now that you have created the upload task, you can upload the file data by sending bytes to the returned upload URL."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "mh6mdW_YTsxv"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"file\": {\n",
            "    \"name\": \"files/4if4o2bqvugf\",\n",
            "    \"displayName\": \"Big Buck Bunny\",\n",
            "    \"mimeType\": \"video/mp4\",\n",
            "    \"sizeBytes\": \"64657027\",\n",
            "    \"createTime\": \"2024-08-26T08:24:56.068012Z\",\n",
            "    \"updateTime\": \"2024-08-26T08:24:56.068012Z\",\n",
            "    \"expirationTime\": \"2024-08-28T08:24:56.049455995Z\",\n",
            "    \"sha256Hash\": \"Zjc4ZjM5NjAzZTY3NzQ5MDdmMmZhYWZhYmYyNmE2NjdmNGE2ZmMzMTc2OWVjMzA0YThhOGY3YzYyZDI4MDUwOA==\",\n",
            "    \"uri\": \"https://generativelanguage.googleapis.com/v1beta/files/4if4o2bqvugf\",\n",
            "    \"state\": \"PROCESSING\"\n",
            "  }\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        ". vars.sh\n",
        "\n",
        "# Extract the upload URL to use from the response headers.\n",
        "upload_url=$(grep -i \"x-goog-upload-url: \" upload-header.tmp | cut -d\" \" -f2 | tr -d \"\\r\")\n",
        "# The header contains our API key, so don't leave it lying around.\n",
        "rm upload-header.tmp\n",
        "\n",
        "# Upload the actual bytes.\n",
        "curl \"${upload_url}\" \\\n",
        "  -H \"Content-Length: ${NUM_BYTES}\" \\\n",
        "  -H \"X-Goog-Upload-Offset: 0\" \\\n",
        "  -H \"X-Goog-Upload-Command: upload, finalize\" \\\n",
        "  --data-binary \"@${VIDEO_PATH}\" >file_info.json 2>/dev/null\n",
        "\n",
        "# Show the output. You will use it in a later step.\n",
        "cat file_info.json"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "3YNSxTqfwmBH"
      },
      "source": [
        "### Get file info\n",
        "\n",
        "After uploading the file, you can verify the API has successfully received the files by querying the [`files.get` endpoint](https://ai.google.dev/api/files#method:-files.get).\n",
        "\n",
        "`files.get` lets you see the file uploaded to the File API that are associated with the Cloud project your API key belongs to. Only the `name` (and by extension, the `uri`) are unique."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "id": "ddq7nfKKxj9C"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"name\": \"files/4if4o2bqvugf\",\n",
            "  \"displayName\": \"Big Buck Bunny\",\n",
            "  \"mimeType\": \"video/mp4\",\n",
            "  \"sizeBytes\": \"64657027\",\n",
            "  \"createTime\": \"2024-08-26T08:24:56.068012Z\",\n",
            "  \"updateTime\": \"2024-08-26T08:25:03.977029Z\",\n",
            "  \"expirationTime\": \"2024-08-28T08:24:56.049455995Z\",\n",
            "  \"sha256Hash\": \"Zjc4ZjM5NjAzZTY3NzQ5MDdmMmZhYWZhYmYyNmE2NjdmNGE2ZmMzMTc2OWVjMzA0YThhOGY3YzYyZDI4MDUwOA==\",\n",
            "  \"uri\": \"https://generativelanguage.googleapis.com/v1beta/files/4if4o2bqvugf\",\n",
            "  \"state\": \"ACTIVE\",\n",
            "  \"videoMetadata\": {\n",
            "    \"videoDuration\": \"596s\",\n",
            "    \"videoThumbnailBytes\": \"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAC0AUADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD+Eeiiiuc88KKKKACiiigAooooAKKKKACiiigAooooAKK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AQzfXpx+B4zTZlJ2k8DnB656Z47Y4+uabl19Fz6jOfzxQTpp+K27dd3ffTboPf5cIfvLkn/gWP8KE4YEckEYX16557flzTDySfXBP17nPvxx2qfGc9eGYDH95cYb8Mnj9aA63XRJ+lrL8HodDRRRXQeuFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUrLsvuQrLsvuQUUUUWXZfcgsuy+5BRRRRZdl9yCy7L7kFc5MxE0gB9Ow96KK0ppa6Lp0XmZ1IxdrxT33S8hLYB4xuAOAuM9s5/wFWI+/wCH9aKKKiWmi69F5GXLH+WP3L/IePvpwD1HI9cVdZRlhzwWP6LRRWFTp8/0MFvL1/RFCT/WL9W/9CWmnp/21X+YoorQ6Rh++34fzagnCNwDyvUZ9aKKAIV/i/3T/SkkVVBwAMg5x7CiigCSZQrYA6EjJ5J6Yye+M8VEfuN+H8moooAliRX3ZzxjGD65/wAKfH3/AOuY/rRRQB//2Q==\"\n",
            "  }\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        ". vars.sh\n",
        "\n",
        "file_uri=$(jq -r \".file.uri\" file_info.json)\n",
        "\n",
        "curl \"${file_uri}?key=${GOOGLE_API_KEY}\" 2>/dev/null"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZPcwr0hkeBcW"
      },
      "source": [
        "### Wait for processing\n",
        "\n",
        "Once the file is uploaded, the file service will perform some pre-processing to prepare the video for use in the LLM. For simple media types this is typically a negligible amount of time, but for video content you may need a short wait.\n",
        "\n",
        "You can use the `state` field to query if the video is ready for use. If you use it in a prompt prematurely you will see an error like `The File ... is not in an ACTIVE state and usage is not allowed`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "iynVhwpibm0a"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Processing video...\n",
            "Processing video...\n",
            "Processing video...\n",
            "Video is now ACTIVE.\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        "\n",
        "state=$(jq -r \".file.state\" file_info.json)\n",
        "file_uri=$(jq -r \".file.uri\" file_info.json)\n",
        "\n",
        "while [[ \"${state}\" == \"PROCESSING\" ]];\n",
        "do\n",
        "  echo \"Processing video...\"\n",
        "  sleep 5\n",
        "  # Get the file of interest to check state\n",
        "  curl \"${file_uri}?key=${GOOGLE_API_KEY}\" >file_info.json 2>/dev/null\n",
        "  state=$(jq -r \".state\" file_info.json)\n",
        "done\n",
        "\n",
        "echo \"Video is now ${state}.\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "62OdL_fzi9FX"
      },
      "source": [
        "### Prompt with the video\n",
        "\n",
        "Now that the video is uploaded and processed, you can reference it in a prompt.\n",
        "\n",
        "When assembling your [`contents`](https://ai.google.dev/api/generate-content#request-body), the video can be referenced using a `file_data` part, like this:\n",
        "\n",
        "```json\n",
        "{\n",
        "  \"file_data\": {\n",
        "    \"mime_type\": \"video/mp4\",\n",
        "    \"file_uri\": \"https://uri/from/previous/steps\"\n",
        "  }\n",
        "}\n",
        "```\n",
        "\n",
        "Try it yourself with this request."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "62dc7ceec783"
      },
      "outputs": [],
      "source": [
        "MODEL_ID = \"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\", \"gemini-2.5-flash\", \"gemini-2.5-pro\",\"gemini-3-pro-preview\"] {\"allow-input\":true, isTemplate: true}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dHLieJDLhLXn"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[1;39m{\n",
            "  \u001b[0m\u001b[34;1m\"parts\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[1;39m[\n",
            "    \u001b[1;39m{\n",
            "      \u001b[0m\u001b[34;1m\"text\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"This is a 3D animated short film about a big bunny that gets tired of his usual diet of grass. He wants to try something different like fruit and learns the value of what he has. The film's title is Big Buck Bunny. \"\u001b[0m\u001b[1;39m\n",
            "    \u001b[1;39m}\u001b[0m\u001b[1;39m\n",
            "  \u001b[1;39m]\u001b[0m\u001b[1;39m,\n",
            "  \u001b[0m\u001b[34;1m\"role\"\u001b[0m\u001b[1;39m: \u001b[0m\u001b[0;32m\"model\"\u001b[0m\u001b[1;39m\n",
            "\u001b[1;39m}\u001b[0m\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        ". vars.sh\n",
        "\n",
        "file_uri=$(jq \".uri\" file_info.json)\n",
        "\n",
        "curl \"${BASE_URL}/v1beta/models/${MODEL_ID}:generateContent?key=${GOOGLE_API_KEY}\" \\\n",
        "    -H 'Content-Type: application/json' \\\n",
        "    -X POST \\\n",
        "    -d '{\n",
        "      \"contents\": [{\n",
        "        \"parts\":[\n",
        "          {\"text\": \"Please describe this file.\"},\n",
        "          {\"file_data\": {\n",
        "            \"mime_type\": \"'${MIME_TYPE}'\",\n",
        "            \"file_uri\": '${file_uri}'}}]\n",
        "        }]\n",
        "       }' 2>/dev/null >response.json\n",
        "\n",
        "jq -C .candidates[].content response.json"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sQuhFLRK03UR"
      },
      "source": [
        "## Further reading\n",
        "\n",
        "The File API lets you upload a variety of multimodal MIME types, including images, audio, and video formats. The File API handles inputs that can be used to generate content with the [content generation endpoint](https://ai.google.dev/api/generate-content).\n",
        "\n",
        "* Read the [`File API`](https://ai.google.dev/api/files) reference.\n",
        "\n",
        "* Learn more about prompting with [media files](https://ai.google.dev/tutorials/prompting_with_media) in the docs, including the supported formats and maximum length."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [
        "Tce3stUlHN0L"
      ],
      "name": "Video_REST.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
